package org.bozo.im.common.types;

import android.os.Parcel;
import android.os.Parcelable;

public class ConnectionID implements Parcelable {

	// ---------------------------------------------------------------------------------------
	// Variables
	// ---------------------------------------------------------------------------------------
	
	private Protocol mProtocol;
	private String mId;

	// ---------------------------------------------------------------------------------------
	// Constructor
	// ---------------------------------------------------------------------------------------
	
	public ConnectionID(Protocol pProtocol, String pId) {
		mProtocol = pProtocol;
		mId = pId;
	}
	
	// ---------------------------------------------------------------------------------------
	// Getters
	// ---------------------------------------------------------------------------------------
	
	public Protocol getProtocol() {
		return mProtocol;
	}

	public String getId() {
		return mId;
	}

	// ---------------------------------------------------------------------------------------
	// Utils methods
	// ---------------------------------------------------------------------------------------
	
	@Override
	public boolean equals(Object pObj) {
		return (pObj instanceof ConnectionID
				&& getId().equals(((ConnectionID)pObj).getId())
				&& getProtocol() == ((ConnectionID)pObj).getProtocol());
	}
	
	@Override
	public int hashCode() {
		return getId().hashCode() & getProtocol().hashCode();
	}
	
	// ---------------------------------------------------------------------------------------
	// Parcelable related methods
	// ---------------------------------------------------------------------------------------
	
	public int describeContents() {
		return 0;
	}

	public void writeToParcel(Parcel pDest, int pFlags) {
		pDest.writeParcelable(getProtocol(), 0);
		pDest.writeString(getId());
	}

	public static final Parcelable.Creator<ConnectionID> CREATOR = new Parcelable.Creator<ConnectionID>() {

		public ConnectionID createFromParcel(Parcel pSource) {
			
			Protocol lProtocol = (Protocol)pSource.readParcelable(getClass().getClassLoader());
			String lId = pSource.readString();
			
			return new ConnectionID(lProtocol, lId);
		}

		public ConnectionID[] newArray(int pSize) {
			return new ConnectionID[pSize];
		}
	};
}
